package com.session5;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;

import net.miginfocom.swing.MigLayout;

public class JComboObject extends JFrame implements ItemListener{
	private JComboBox cboStudent;
	private JList listStudent;
	private Vector<Student> vtrData = null;
	public JComboObject() {
		vtrData = new Vector<Student>();
		vtrData.addElement(new Student("Pham Van Viet","HD",21));
		vtrData.addElement(new Student("Pham Dang Duc","HP",23));
		vtrData.addElement(new Student("Nguyen Van An","ND",24));
	}
	public void createForm(){
		this.setLayout(new MigLayout());
		this.cboStudent = new JComboBox(vtrData);
		this.listStudent = new JList(vtrData);
		this.getContentPane().add(this.cboStudent,"wrap");
		this.getContentPane().add(this.listStudent,"wrap");
		
		//Dang ki su kien
		this.cboStudent.addItemListener(this);
	}
	public void showForm(){
		this.setTitle("JComboBox");
		this.setSize(400,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		JComboObject f= new JComboObject();
		f.createForm();
		f.showForm();
	}
	@Override
	public void itemStateChanged(ItemEvent e) {
		if(e.getSource() == this.cboStudent){
			if(e.getStateChange() == ItemEvent.SELECTED){
				Student s = (Student)this.cboStudent.getSelectedItem();
				System.out.println(s.getName() + ">>" + s.getAge());
			}
		}
	}

}
